package com.codebattery.controller;

import com.codebattery.config.RabbitMQConfig;
import com.codebattery.domain.model.SecOrder;
import com.codebattery.service.OrderService;
import com.rabbitmq.client.Channel;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Controller;

import java.util.Map;

@Controller
public class SecOrderController {
    private static final Logger LOGGER = LoggerFactory.getLogger(SecOrderController.class);
    @Autowired
    private OrderService orderService;

    /**
     * 监听订单消息队列，并消费
     *
     */
    @RabbitListener(queues = RabbitMQConfig.ORDER_QUEUE)
    public void createOrder(@Payload JSONObject jsonObject, Channel channel, @Headers Map<String, Object> headers) {

        SecOrder secOrder = (SecOrder) JSONObject.toBean(jsonObject, SecOrder.class);

        LOGGER.info("收到订单消息，订单用户为：{}，商品名称为：{}", secOrder.getUserId(), secOrder.getGoodsId());
        /**
         * 调用数据库orderService创建订单信息
         */
//        orderService.createOreder(order);
    }
}